/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.activities.dashboard;

import java.io.File;

import android.content.Intent;
import android.graphics.Bitmap;
import android.location.Address;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.TextView;

import com.phonebooksharing.android.R;
import com.phonebooksharing.android.util.GalleryUtilities;
import com.phonebooksharing.android.util.IOUtilities;
import com.phonebooksharing.android.util.LocationUtil;

public class PhotoReaderActivity extends AbstractReaderActivity {

    public static final String PHOTO_WIDTH           = "w",
                               PHOTO_HEIGHT          = "h",
                               PHOTO_ID              = "id",
                               PHOTO_LONGITUDE       = "lo",
                               PHOTO_LATITUDE        = "la",
                               PHOTO_TITLE           = "tl",
                               PHOTO_FILENAME        = "fn";
    
//    private int photoW, photoH, screenW, screenH;
//    private long photoID;
    private View container;
    private ImageView photo;
    private TextView title, where, resolution;
    private File fileName;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        // Handle the calling intent
        final Intent i = getIntent();
        container = ((ViewStub)findViewById(R.id.photoStub)).inflate();
        
        photo = (ImageView)container.findViewById(R.id.icon);
        photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                invokeImageReader();
            }
        });
        
        title = (TextView)container.findViewById(R.id.text1);
        where = (TextView)container.findViewById(R.id.text2);
        resolution = (TextView)container.findViewById(R.id.text3);
        
        ((TextView)findViewById(R.id.title)).setText("Image Thumbnail");
         
        fileName = new File(i.getStringExtra(PHOTO_FILENAME));
        
        new CreateThumbnail().execute();
        
        invokeImageReader();
    }

    private void invokeImageReader() {
        Intent av = new Intent(Intent.ACTION_VIEW);
        av.setDataAndType(Uri.fromFile(fileName), "image/*");
        startActivity(av);
    }
    
    class CreateThumbnail extends AsyncTask<Void, Object, Void> {
//        private View loading, text;
        
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
//            loading = container.findViewById(R.id.loadingStub);
//            text    = container.findViewById(R.id.text1);

//            text.setVisibility(View.GONE);
//            loading.setVisibility(View.VISIBLE);
        }

        @Override
        protected Void doInBackground(Void... params) {
            final Intent i = getIntent();
            final String lat    = i.getStringExtra(PHOTO_LATITUDE),
                         lon    = i.getStringExtra(PHOTO_LONGITUDE),
                         title  = i.getStringExtra(PHOTO_TITLE)
                         ;
            final Integer width  = i.getIntExtra(PHOTO_WIDTH, 0),
                          height = i.getIntExtra(PHOTO_HEIGHT, 0);

            String where = "";
            try {
                Address a = LocationUtil.getAddress(getApplicationContext(), new Double(lat), new Double(lon));
                where = a.getPostalCode() + " " + a.getLocality() + " " + a.getCountryName();
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            publishProgress(GalleryUtilities.getBitmapFromFile(getContentResolver(), fileName), title, where, width, height);
            return null;
        }

        @Override
        protected void onProgressUpdate(Object... values) {
            super.onProgressUpdate(values);

            photo.setImageBitmap((Bitmap)values[0]);
            title.setText("" + values[1]);
            where.setText("" + values[2]);
            resolution.setText("" + values[3] + " x " + values[4]);
        }
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        IOUtilities.removeExternalFile(fileName);
    }
}
